/*
 * ToolButtonDelegate.cpp  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ToolButtonDelegate.h"

#include "MenuDelegate.h"

// ToolButtonDelegate :: Protected Constructor. --------------------------------

/*!
 * \brief Constructs new ToolButtonDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the ToolButton java object
 */
ToolButtonDelegate::ToolButtonDelegate(JNIEnv *env, jobject obj)
  : AbstractButtonDelegate(env, obj, new QToolButton())
{
  qToolButton()->setAutoRaise(true);
  qToolButton()->setFocusPolicy(Qt::NoFocus);
}

// ToolButtonDelegate :: Native Calls. -----------------------------------------

/*!
 * \brief Sets menu.
 *
 * \param env the JNIEnv pointer
 * \param menuDelegate the menu delegate
 */
void ToolButtonDelegate::setMenu(JNIEnv *env, jlong menuDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setMenu, SWING_ENV_ARG, JLONG_ARG(menuDelegate))
  if (menuDelegate) {
    qToolButton()->setMenu(MENU_CAST(menuDelegate)->qMenu());
  } else {
    qToolButton()->setMenu(0);
  }
}

/*!
 * \brief Sets popup mode.
 *
 * \param env the JNIEnv pointer
 * \param popupMode the popup mode
 */
void ToolButtonDelegate::setPopupMode(JNIEnv *env, jint popupMode)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setPopupMode, SWING_ENV_ARG, JINT_ARG(popupMode))
  qToolButton()->setPopupMode(QToolButton::ToolButtonPopupMode(popupMode));  // TODO: check cast ?
}

/*!
 * \brief Sets tool button style.
 *
 * \param env the JNIEnv pointer
 * \param toolButtonStyle the tool button style
 */
void ToolButtonDelegate::setToolButtonStyle(JNIEnv *env, jint toolButtonStyle)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setToolButtonStyle, SWING_ENV_ARG, JINT_ARG(toolButtonStyle))
  qToolButton()->setToolButtonStyle(Qt::ToolButtonStyle(toolButtonStyle));  // TODO: check cast ?
}

// ToolButtonDelegate :: Static Native Calls. ----------------------------------

/*!
 * \brief Returns new instance of the bridge object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the bridge object
 */
jlong ToolButtonDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new ToolButtonDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}
